<?php

namespace tjsd\controllers;

/**
 * 
 * 
 * @author Jakub Tesárek <info@tjsd.cz>
 * @copyright 2012
 * @version 1.0
 */
abstract class Controller extends \tjsd\core\Object {
	const DEFAULT_ACTION = '_';
	const ACTION_SUFFIX = 'Action';
	
	protected $serviceContainer;
	
	public function __construct() {
		
	}
	
	public function setServiceContainer(\tjsd\system\di\ServiceContainer $serviceContainer) {
		$this->serviceContainer = $serviceContainer;
	}
	
	public final function invoke($action = self::DEFAULT_ACTION) {
		$methodName = self::constructMethodName($action);
		if (method_exists($this, $methodName)) {
			$this->{$methodName}();
		} else {
			throw new tjsd\exceptions\UndefinedFunctionException(
				'Controller method ' . $methodName , ' is note defined in ' . __CLASS__ . '.'
			);
		}
	}
	
	protected static function constructMethodName($action) {
		return $action . self::ACTION_SUFFIX;
	}
}